﻿using System.Windows;
using System.ComponentModel;
using System.Collections.Generic;
using System.Linq;

namespace C9ShowNotesHelper
{
  /// <summary>
  /// Interaction logic for FindShow.xaml
  /// </summary>
  public partial class FindShow : Window
  {
    List<Channel9WrapperLibrary.Show> showlist = new List<Channel9WrapperLibrary.Show>();
    int currentshowpage = 1;

    BackgroundWorker _backgroundWorker = new BackgroundWorker();
    
    public Channel9WrapperLibrary.Show SelectedShow { get; set; }

    public FindShow()
    {
      InitializeComponent();
      
     }

    private void okButton_Click(object sender, RoutedEventArgs e)
    {
      CloseForm();
    }

    private void cancelButton_Click(object sender, RoutedEventArgs e)
    {
      CancelForm();
    }

    private void refreshButton_Click(object sender, RoutedEventArgs e)
    {
      RefreshShowList();
    }

    void _backgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
      var showpage = Channel9WrapperLibrary.Shows.GetShowsList((int)e.Argument);
      e.Result = showpage;
    }

    void _backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
       
      if (e.Cancelled)
      {
        //statusText.Text = "Cancelled";
      }
      else if (e.Error != null)
      {
        //statusText.Text = "Exception Thrown";
      }
      else
      {
        //statusText.Text = "Completed";
        List<Channel9WrapperLibrary.Show> showpage = (List<Channel9WrapperLibrary.Show>)e.Result;
        showlist.AddRange(showpage);
        listBox1.ItemsSource = showlist;
        listBox1.Items.Refresh();

        if (_backgroundWorker.CancellationPending)
          return;

        if (showpage.Count>0 )
        {
          currentshowpage += 1;
          statusLabel.Content = "Loading Show Page " + currentshowpage.ToString();
          _backgroundWorker.RunWorkerAsync(currentshowpage);
        }
        else
          statusLabel.Content = "";
      }
    }

    private void listBox1_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
     
       CloseForm();
    }

    void CloseForm()
    {
      if (listBox1.SelectedItem != null)
        SelectedShow = (Channel9WrapperLibrary.Show)listBox1.SelectedItem;

      if (_backgroundWorker.IsBusy)
        _backgroundWorker.CancelAsync();

      this.DialogResult = true;
      this.Close();

    }

    void CancelForm()
    {
      if (_backgroundWorker.IsBusy)
        _backgroundWorker.CancelAsync();

      SelectedShow = null;

      this.DialogResult = false;
      this.Close();
    }

    void RefreshShowList()
    {
      listBox1.ItemsSource = null;
      listBox1.Items.Refresh();

      currentshowpage = 1;
      showlist.Clear();

      _backgroundWorker.RunWorkerAsync(currentshowpage);
    }

    private void filterTextBox_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
    {
      var results = showlist
        .Where(d => d.Title.ToUpperInvariant().Contains(filterTextBox.Text.ToUpperInvariant()))
        .ToList();

      listBox1.ItemsSource = null;
      listBox1.ItemsSource = results;
      listBox1.Items.Refresh();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
      _backgroundWorker.DoWork += _backgroundWorker_DoWork;
      _backgroundWorker.RunWorkerCompleted += _backgroundWorker_RunWorkerCompleted;
      _backgroundWorker.WorkerSupportsCancellation = true;

      statusLabel.Content = "Loading Show Page " + currentshowpage.ToString();

      _backgroundWorker.RunWorkerAsync(currentshowpage);

    }



  }
}
